<?php namespace system\model;
use houdunwang\model\Model;
class Article extends Model{
	//数据表
	protected $table = "article";

	//允许填充字段
	protected $allowFill = [ ];

	//禁止填充字段
	protected $denyFill = [ ];

	//自动验证
	protected $validate=[
		//['字段名','验证方法','提示信息',验证条件,验证时间]
		['title','isnull','请填写标题',self::MUST_VALIDATE,self::MODEL_BOTH],
		['author','isnull','请填写文章作者',self::MUST_VALIDATE,self::MODEL_BOTH],
		['category_cid','isnull','请选择文章分类',self::MUST_VALIDATE,self::MODEL_BOTH],
		['thumb','isnull','请上传缩略图',self::MUST_VALIDATE,self::MODEL_BOTH],
		['digest','isnull','请填写文章摘要',self::MUST_VALIDATE,self::MODEL_BOTH],
		['keywords','isnull','请填写关键字',self::MUST_VALIDATE,self::MODEL_BOTH],
	];

	//自动完成
	protected $auto=[
		//['字段名','处理方法','方法类型',验证条件,验证时机]
		['sendtime','time','function',self::MUST_AUTO,self::MODEL_INSERT],
		['updatetime','time','function',self::MUST_AUTO,self::MODEL_UPDATE],
	   //getUid使我们自定义的方法，在下面
		['user_uid','getUid','method',self::MUST_AUTO,self::MODEL_BOTH],
	];
   public function getUid($val,$data)
   {
	  //return md5($val);
	  return Session::get('uid');
   }
	//添加
   public function store(){
//      p($_POST);die;
	  if(!isset($_POST['tag'])){
		 return ['valid'=>0,'message'=>'请选择标签'];
	  }
	  //1.添加文章表
	  $this->title = $_POST['title'];
	  $this->author = $_POST['author'];
	  $this->category_cid = $_POST['category_cid'];
	  $this->thumb = $_POST['thumb'];
	  $this->digest = $_POST['digest'];
	  $this->keywords = $_POST['keywords'];
	  $aid = $this->save();
	  //添加文章数据表
	  $artitleDataModel=new ArticleData();
	  $artitleDataModel->des = $_POST['des'];
	  $artitleDataModel->content = $_POST['content'];
	  $artitleDataModel->article_aid = $aid;
	  $artitleDataModel->save();
	  //文章中间表
	  $articleTagModel = new ArticleTag();
	  //做判断，验证文章标签必须得选择,判断放在最上面
	  foreach($_POST['tag'] as $v)
	  {
		 $articleTagModel->article_aid = $aid;
		 $articleTagModel->tag_tid = $v;
		 $articleTagModel->save();
	  }
	  return ['valid'=>1,'message'=>'操作成功'];
   }
   //获取分页及数据
   public function getAllData($num,$is_recycle=0){
      //页面属性
      $count=Db::table('article')->where('is_recycle',$is_recycle)->count();
//      p($count);
	  Page::row($num)->make($count);
//	  p($page);
	  $page=Page::desc(['pre'=>'上页', 'next'=>'下页','first'=>'首页','end'=>'尾页','unit'=>'个'])->make($count,$num);
//	  p($page);
	  //数据
	  $data = Db::table('article')
		  ->join('category','article.category_cid','=','category.cid')
		  ->where('is_recycle',$is_recycle)->limit(Page::limit())
		  ->get();
//	  p($data);
	  return ['page'=>$page,'data'=>$data];
   }
   //获取单条旧数据
   public function getOldData($aid){
	  return Db::table('article')
		  ->join('article_data','article.aid','=','article_data.article_aid')
		  ->where('article.is_recycle',0)->where('article.aid','=',$aid)->first();
   }

   /**  编辑功能
	* @param $aid 修改的文章主键
	*/
   public function edit($aid){
      //文章表的修改
	  if(!isset($_POST['tag'])){
		 return ['valid'=>0,'message'=>'请选择标签'];
	  }
	  $modelArticle=$this->find($aid);
	  $modelArticle->title = $_POST['title'];
	  $modelArticle->author = $_POST['author'];
	  $modelArticle->category_cid = $_POST['category_cid'];
	  $modelArticle->thumb = $_POST['thumb'];
	  $modelArticle->digest = $_POST['digest'];
	  $modelArticle->keywords = $_POST['keywords'];
	  $modelArticle->save();
	  //文章数据表的修改
	  $ad_id = Db::table('article_data')->where("article_aid",$aid)->pluck('ad_id');
	  $articleDataModel = ArticleData::find($ad_id);
	  $articleDataModel->des = $_POST['des'];
	  $articleDataModel->content = $_POST['content'];
	  $articleDataModel->article_aid = $aid;
	  $articleDataModel->save();
	  //标签中间表修改
	  $articleTagModel = new ArticleTag();
	  //1.在文章标签中间表中，删除原来当前文章的相关数据
	  $articleTagModel->where('article_aid',$aid)->delete();
	  //2.新增
	  foreach($_POST['tag'] as $v)
	  {
		 $articleTagModel->article_aid = $aid;
		 $articleTagModel->tag_tid = $v;
		 $articleTagModel->save();
	  }
	  return ['valid'=>1,'message'=>'操作成功'];
   }
   //彻底删除数据
   public function delall($aid){
	  //1.删除文章表
	  $this->where('aid',$aid)->delete();
	  //2.删除文章数据表
	  (new ArticleData())->where('article_aid',$aid)->delete();
	  //3.删除文章标签中间表
	  (new ArticleTag())->where('article_aid',$aid)->delete();
	  return true;
   }

	//获取所有标签分类
    public function getCateData(){
	   return Arr::tree(Db::table('category')->get(),'cname');
	}
	//获取所有标签数据
    public function getTagData()
    {
	  //获取标签表中所有数据
	  return Db::table('tag')->get();
    }
}